package com.braain.states;

import com.braain.entities.Player;
import com.braain.level.BlockMap;
import com.braain.level.LevelObject;
import com.braain.metroid.Keyboard;
import com.braain.metroid.Weasel;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class GameState extends BasicGameState {

  //private BlockMap bmap;
  //private Keyboard keyb = Keyboard.getInstance();
  //private BlockMap bmap = BlockMap.getInstance();
  //private Player samus = Player.getInstance();
  //private final Weasel weasel = Weasel.getInstance();
  //private boolean debug=true;
  public static final int ID = 2;

  public int getID() {
    return ID;
  }

  public void init(GameContainer container, StateBasedGame game) throws SlickException {
    container.setVSync(true);
    //container.setTargetFrameRate(60);
    //container.setMaximumLogicUpdateInterval(10);
    BlockMap.getInstance().newMap("data/level/level0001.tmx");
    container.setShowFPS(true);
  }

  public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {

    Keyboard.pollKeyboard(container);
    Player.getInstance().update(delta);
    BlockMap.getInstance().update(delta);

    if (Keyboard.isEsc()) {
      System.exit(0);
    }
    if (Keyboard.isShoot()){
      Weasel.setDebug(!Weasel.isDebug());
    }
    if (Keyboard.isSelect()){
      System.out.println("mapx=["+BlockMap.getMapX()+"] mapY=["+BlockMap.getMapY()+"]\n"
        + " samX=["+Player.getInstance().getX()+"] samY=["+Player.getInstance().getY()+"]\n"
        + "mapLeftBorder=["+BlockMap.getInstance().isMapLeftBorder()+"] mapRightBorder=["+BlockMap.getInstance().isMapRightBorder()+"]\n"
        + "mapTopBorder=["+BlockMap.getInstance().isMapTopBorder()+"] mapBottomBorder=["+BlockMap.getInstance().isMapBottomBorder()+"]\n"
        + "=====================");
    }
  }

  public void render(GameContainer container, StateBasedGame game, Graphics g) {

    BlockMap.getInstance().drawScenery(g);
    BlockMap.getInstance().drawBackgound(g);
    if (!Weasel.isDebug())
      Player.getInstance().draw(g);
    BlockMap.getInstance().drawForeground(g);

    if (Weasel.isDebug()) {
      // =============================
      // display collision, coords data
      // =============================
      for (LevelObject entity : BlockMap.getInstance().getEntities()) {
        g.translate(BlockMap.getInstance().getMapX(), BlockMap.getInstance().getMapY());
        entity.draw(g);
        g.resetTransform();
      }
      Player.getInstance().draw(g);
    }
  }
}
